package org.jeecg.modules.demo.hd.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.hd.entity.AppProject;
import org.jeecg.modules.demo.hd.service.IAppMyProjectDetailService;
import org.jeecg.modules.demo.hd.service.IAppMyProjectService;
import org.jeecg.modules.demo.hd.service.IAppNewsService;
import org.jeecg.modules.demo.hd.service.IAppProjectService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.hd.vo.myProject.MyProjectDetailDayVo;
import org.jeecg.modules.demo.hd.vo.myProject.ProjectDetailVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 项目管理
 * @Author: jeecg-boot
 * @Date: 2025-04-13
 * @Version: V1.0
 */
@Api(tags = "app_project")
@RestController
@RequestMapping("/hd/appProject")
@Slf4j
public class AppProjectController extends JeecgController<AppProject, IAppProjectService> {
    @Autowired
    private IAppProjectService appProjectService;

    @Autowired
    private IAppMyProjectService myProjectServicel;

    @Autowired
    private IAppMyProjectDetailService myProjectDetailService;

    /**
     * 分页列表查询
     *
     * @param appProject
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "app_project-分页列表查询")
    @ApiOperation(value = "app_project-分页列表查询", notes = "app_project-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppProject>> queryPageList(AppProject appProject,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<AppProject> queryWrapper = QueryGenerator.initQueryWrapper(appProject, req.getParameterMap());
        Page<AppProject> page = new Page<AppProject>(pageNo, pageSize);
        IPage<AppProject> pageList = appProjectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @ApiOperation(value = "获取精品项目", notes = "获取精品项目")
    @GetMapping(value = "/queryJpList")
    public Result<List<AppProject>> queryJpList() {
        LambdaQueryWrapper<AppProject> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(AppProject::getProType, 0);
        List<AppProject> pageList = appProjectService.list(lambdaQueryWrapper);
        return Result.OK(pageList);
    }



    @ApiOperation(value = "获取热门项目", notes = "获取热门项目")
    @GetMapping(value = "/queryRmList")
    public Result<List<AppProject>> queryRmList() {
        LambdaQueryWrapper<AppProject> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(AppProject::getProType, 1);
        List<AppProject> pageList = appProjectService.list(lambdaQueryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 添加
     *
     * @param appProject
     * @return
     */
    @AutoLog(value = "app_project-添加")
    @ApiOperation(value = "app_project-添加", notes = "app_project-添加")
    @RequiresPermissions("hd:app_project:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AppProject appProject) {
        appProjectService.save(appProject);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appProject
     * @return
     */
    @AutoLog(value = "app_project-编辑")
    @ApiOperation(value = "app_project-编辑", notes = "app_project-编辑")
    @RequiresPermissions("hd:app_project:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AppProject appProject) {
        appProjectService.updateById(appProject);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app_project-通过id删除")
    @ApiOperation(value = "app_project-通过id删除", notes = "app_project-通过id删除")
    @RequiresPermissions("hd:app_project:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appProjectService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "app_project-批量删除")
    @ApiOperation(value = "app_project-批量删除", notes = "app_project-批量删除")
    @RequiresPermissions("hd:app_project:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.appProjectService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 获取项目详情
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "app_project-通过id查询")
    @ApiOperation(value = "获取项目详情", notes = "获取项目详情")
    @GetMapping(value = "/queryById")
    public Result<ProjectDetailVo> queryById(@RequestParam(name = "id", required = true) String id) {
        AppProject appProject = appProjectService.getById(id);
        if (appProject == null) {
            return Result.error("未找到对应数据");
        }
        ProjectDetailVo vo = new ProjectDetailVo();
        BeanUtils.copyProperties(appProject, vo);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        boolean b = myProjectServicel.haveState(sysUser.getId(), id);
        vo.setLqState(b);
        return Result.OK(vo);
    }

    @ApiOperation(value = "获取项目本月完成情况", notes = "获取项目本月完成情况")
    @GetMapping(value = "/queryDetailByProjectId")
    public Result<List<MyProjectDetailDayVo>> queryDetailByProjectId(@RequestParam(name = "id", required = true) String id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<MyProjectDetailDayVo> list = myProjectDetailService.queryByProjectId(sysUser.getId(), id);
        return Result.OK(list);
    }
}
